import { tt } from "../../ttapi_interface/ttapi.js";
import { Atals_Editor } from "./atlas/creator.js";

import { IFileEditor } from "./editorbase.js";
import { Image_Editor } from "./image/creator.js";
import { Text_Editor } from "./text/creator.js";


export class EditorManager {
    private static _init: boolean = false;;
    private static Init() {
        if (this._init)
            return;

        this.RegEditor(new Text_Editor());
        this.RegEditor(new Image_Editor());
        this.RegEditor(new Atals_Editor());
        this._init = true;
    }
    static RegEditor(creator: IFileEditor): void {
        let exts = creator.getCareExts();
        for (var i = 0; i < exts.length; i++) {
            this.mapCreator[exts[i].toLowerCase()] = creator;
        }
    }
    private static mapCreator: { [id: string]: IFileEditor } = {}
    static GetEditor(ext: string): IFileEditor {
        this.Init();
        let _ext = ext.toLowerCase();
        while (_ext != null && _ext != "") {
            if (this.mapCreator[_ext] != undefined)
                return this.mapCreator[_ext];
          
            _ext = tt.PathTool.GetLongExt(_ext.substring(1));
        }
        return null;
    }
    static GetAllEditors(): IFileEditor[] {
        this.Init();
        let editor: IFileEditor[] = [];
        for (var key in this.mapCreator) {
            let e = this.mapCreator[key];
            if (editor.includes(e))
                continue;
            editor.push(e);
        }
        return editor;
    }

}

